﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Infrastructure;
using Enterprise.Model.Kaifabu;
using Enterprise.Service.Kaifabu;
namespace Enterprise.UI.Web.Modules.Kaifa
{
    public partial class Chanliang : Enterprise.Service.BasePage
    {
        protected int year = (int)Utility.sink("year", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        protected string dw = (string)Utility.sink("dw", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (year == 0)
            {
                year = DateTime.Now.Year;
            }
            if (string.IsNullOrEmpty(dw))
            {
                dw = "SZK";
            }
            if (!IsPostBack)
            {
                bindDDL();
                bindGrid();
            }
        }

      

        private void bindDDL()
        {
            int iNow = DateTime.Now.Year;
            int iPre = iNow - 2;
            int iNext = iNow + 2;
            for (int i = iPre; i <= iNext; i++)
            {
                ddl_ny.Items.Add(new ListItem(i.ToString()));
            }
            ListItem liyear = this.ddl_ny.Items.FindByValue(year.ToString());
            if (liyear != null)
                liyear.Selected = true;
            //绑定单位
            DanweiService dws = new DanweiService();
            ddl_dw.DataSource = dws.GetList("");
            if (this.Language == Service.Translation.Language.ru)
            {
                ddl_dw.DataTextField = "DWMCRU";
            }
            else
            {
                ddl_dw.DataTextField = "DWMC";
            }
            ddl_dw.DataValueField = "DWMC";
            ddl_dw.DataBind();
            ListItem lidw = this.ddl_dw.Items.FindByValue(dw.ToString());
            if (lidw != null)
                lidw.Selected = true;
        }

        private void bindGrid()
        {
            ChanliangService cs = new ChanliangService();
            System.Data.DataSet ds = cs.GetList(" Year= " + year + " and dwmc='" + dw + "'");
            if (ds.Tables[0].Rows.Count == 0)
            {
                //如果没有该单位的数据 创建空表               
                cs.Create(year, ddl_dw.SelectedValue);
                GridView1.DataSource = cs.GetList(" Year= " + year + " and dwmc='" + dw + "'");
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            string _note = "操作成功";
            try
            {
                Enterprise.Service.Kaifabu.ChanliangService khbservice = new Service.Kaifabu.ChanliangService();
                string sql = "begin ";
                string updateSql = "update kfb_chanliang set cl={0} where YEAR={1} and DWMC='{2}' and MONTH={3};";
                int i = 1;
                foreach (GridViewRow row in GridView1.Rows)
                {
                    TextBox txtcl = (TextBox)row.Cells[3].FindControl("tb_cl");
                    decimal tx = (decimal)Utility.sink(txtcl.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Decimal);
                    sql += string.Format(updateSql, tx, year, dw, i);
                    i++;
                }
                sql += "null;end;";
                khbservice.Update(sql);
            }
            catch (Exception ex)
            {
                _note = ex.Message;
            }
            Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "Chanliang.aspx?year=" + year + "&dw=" + dw);
        }

        protected void ddl_ny_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectIndexChanged();
        }

        protected void ddl_dw_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectIndexChanged();
        }

        private void SelectIndexChanged()
        {
            int _year = (int)Utility.sink(ddl_ny.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
            string _dw = (string)Utility.sink(ddl_dw.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
            Response.Redirect("Chanliang.aspx?year=" + _year + "&dw=" + _dw + "");
            Response.End();
        }

    }
}